package com.mijie.homi.dao.timeline;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.mijie.core.IBatisDao;
import com.mijie.homi.common.domain.timeline.TimelineEvent;
import com.mijie.homi.common.util.HomiUtil;
import com.mijie.homi.domain.timeline.TimelineEventDynamic;

@Repository
public class TimelineDao extends IBatisDao {

	public int addTimelineEvent(TimelineEvent event){
		Map<String,Object> parameter = new HashMap<String,Object>(2);
		parameter.put("tableName", HomiUtil.getTimelineTableName(event.getUserId()));
		parameter.put("event", event);
		return this.insert("addTimelineEvent",parameter);
	}
	
	public int deleteTimelineEvent(int userId,int id){
		Map<String,Object> parameter = new HashMap<String,Object>(3);
		parameter.put("tableName", HomiUtil.getTimelineTableName(userId));
		parameter.put("userId", userId);
		parameter.put("id", id);
		return this.delete("deleteTimelineEvent", parameter);
	}
	
	public TimelineEvent getTimelineEvent(int userId,int id){
		Map<String,Object> parameter = new HashMap<String,Object>(2);
		parameter.put("tableName", HomiUtil.getTimelineTableName(userId));
		parameter.put("id", id);
		return this.selectOne("getTimelineEvent",parameter);
	}
	
	public List<TimelineEvent> timelineSegment(int userId,Integer sinceId,Integer maxId,int count){
		Map<String,Object> parameter = new HashMap<String,Object>(5);
		parameter.put("tableName", HomiUtil.getTimelineTableName(userId));
		parameter.put("userId", userId);
		if(sinceId!=null) parameter.put("sinceId", sinceId);
		if(maxId!=null){
			if(sinceId!=null) parameter.remove("sinceId");
			parameter.put("maxId", maxId);
		}
		parameter.put("count", count);
		return this.selectList("timelineSegment", parameter);
	}
	
}
